package com.colorful.insuranceproject.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.colorful.insuranceproject.R;
import com.colorful.insuranceproject.bean.DrugAddressBean;
import com.lovely3x.common.adapter.BaseViewHolder;
import com.lovely3x.common.adapter.ListAdapter;

import java.util.List;

import butterknife.Bind;

/**
 * Created by handsome-Bob on 2017/8/18.
 * <p>
 * 药店地址的适配器
 */
public class DrugAddressAdapter extends ListAdapter<DrugAddressBean> {

    public DrugAddressAdapter(List<DrugAddressBean> datas, Context context) {
        super(datas, context);
    }

    @NonNull
    @Override
    public BaseViewHolder createViewHolder(int position, ViewGroup parent) {
        return new ViewHolder(getLayoutInflater().inflate(R.layout.item_drug_address, parent, false));
    }

    @Override
    public void handleData(int position, @NonNull BaseViewHolder holder) {
        ViewHolder viewHolder = (ViewHolder) holder;
        DrugAddressBean bean = datas.get(position);

        viewHolder.tvStoreName.setText(mContext.getString(R.string.address_desc, bean.getName(), bean.getShopName()));

        viewHolder.tvAddress.setText(mContext.getString(R.string.address_, bean.getAddress()));

        if (bean.getDistance() >= 1000) {
            viewHolder.tvDistance.setText(String.format(getContext().getString(R.string.format_distance_km), bean.getDistance() / 1000));
        } else {
            viewHolder.tvDistance.setText(String.format(getContext().getString(R.string.format_distance_m), bean.getDistance()));
        }

        viewHolder.lineView.setVisibility(getCount() - 1 == position ? View.GONE : View.VISIBLE);
    }

    class ViewHolder extends BaseViewHolder {

        @Bind(R.id.tv_store_name)
        TextView tvStoreName;

        @Bind(R.id.tv_address)
        TextView tvAddress;

        @Bind(R.id.tv_distance)
        TextView tvDistance;

        @Bind(R.id.view_line)
        View lineView;

        public ViewHolder(View rootView) {
            super(rootView);
        }
    }

}
